package com.sftz.modules.minapp.teachActivityAnswer.module;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.SqlManager;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import com.google.common.collect.Maps;
import com.sftz.assets.entities.SUserEntity;
import com.sftz.assets.entities.teachactivity.TeachActivityAnswerEntity;
import com.sftz.assets.entities.teachactivity.TeachActivityApproveEntity;
import com.sftz.assets.entities.teachactivity.TeachActivityEntity;
import com.sftz.modules.minapp.teachActivityAnswer.form.ActivityDetailForm;
import com.sftz.modules.minapp.teachActivityAnswer.form.ActivityForm;
import com.sftz.modules.minapp.teachActivityAnswer.form.TeachAcFileForm;
import com.sftz.modules.minapp.teachActivityAnswer.service.TeachActivityViewService;
import com.sftz.modules.minapp.user.cache.RedissionUserCache;
import com.uxuexi.core.common.util.DateUtil;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.IDbDao;

/**
 * @author   郝振斌
 * @Date	 2018年12月26日
 */
@IocBean
@At("/wx/teachActivityAnswer")
public class TeachActivityModule {

	@Inject
	private TeachActivityViewService teachActivityViewService;

	@Inject
	private IDbDao dbDao;

	@Inject
	private SqlManager sqlManager;

	/**
	 * @author  郝振斌
	 * @throws IOException 
	 * @Date   2018年12月26日
	 * (effect:课程教学实践课件展示)
	 */
	@At
	@Ok("json")
	public Object actiData(@Param("..") final ActivityForm form) throws IOException {
		return teachActivityViewService.actiData(form);
	}

	@At
	@Ok("json")
	public Object saveFile(@Param("..") final TeachAcFileForm form) {
		return teachActivityViewService.saveFile(form);
	}

	@At
	@Ok("json")
	public Object deleteFile(@Param("id") final Integer fileId) {
		return teachActivityViewService.deleteFile(fileId);
	}

	@At
	@Ok("json")
	@Aop("txDb")
	public Object preserve(@Param("..") final TeachAcFileForm form) {
		Date nowDate = DateUtil.nowDate();
		Integer userId =  RedissionUserCache.getUserId(form.getToken());
		Integer activityId = form.getActivityId();
		TeachActivityEntity teaAc = dbDao.fetch(TeachActivityEntity.class, Cnd.where("id", "=", activityId));
		Date endDate = teaAc.getEndDate();
		TeachActivityAnswerEntity answer = new TeachActivityAnswerEntity();
		if (!Util.isEmpty(endDate)) {
			if (endDate.getTime() < nowDate.getTime()) {
				return 0;
			} else {
				//查询当前用户是否是第一次作答
				TeachActivityAnswerEntity answerInfo = dbDao.fetch(TeachActivityAnswerEntity.class,
						Cnd.where("userId", "=", userId).and("activityId", "=", activityId));
				if (Util.isEmpty(answerInfo)) {
					TeachActivityAnswerEntity eny = new TeachActivityAnswerEntity();
					eny.setActivityId(activityId);
					eny.setContent(form.getData());
					eny.setHandleDate(nowDate);
					eny.setUserId(userId);
					answer = dbDao.insert(eny);
				} else {
					dbDao.update(TeachActivityAnswerEntity.class,
							Chain.make("content", form.getData()).add("handleDate", nowDate),
							Cnd.where("userId", "=", userId).and("activityId", "=", activityId));

					answer = dbDao.fetch(TeachActivityAnswerEntity.class,
							Cnd.where("userId", "=", userId).and("activityId", "=", activityId));
				}

				//审核表添加记录
				Integer answerId = answer.getId();
				TeachActivityApproveEntity enty = dbDao.fetch(TeachActivityApproveEntity.class,
						Cnd.where("answerId", "=", answerId).and("activityId", "=", activityId));
				if (Util.isEmpty(enty)) {
					TeachActivityApproveEntity appeny = new TeachActivityApproveEntity();
					appeny.setActivityId(activityId);
					appeny.setAnswerId(answerId);
					appeny.setStatus(-1);//提交状态
					dbDao.insert(appeny);
				}

				return 1;
			}
		} else {
			return -1;
		}
	}

	/**
	 * @author  郝振斌
	 * @throws IOException 
	 * @Date   2018年12月26日
	 */
	@At
	@Ok("json")
	public Object detailsData(@Param("..") final ActivityForm form) throws IOException {
		return teachActivityViewService.actiData(form);
	}

	
	/**
	 * 活动作答上传文件
	 * @param file
	 * @param fileType
	 * @return
	 * @throws IOException
	 */
	@At
	@AdaptBy(type = UploadAdaptor.class)
	public Object uploadF(@Param("file") TempFile file,@Param("..") final TeachAcFileForm form) throws IOException {
		return teachActivityViewService.uploadFile(file,form);
	}

}